package openbang.account.web.portal.client;

import java.util.ArrayList;

import openbang.account.service.intf.AccountService;
import openbang.account.service.vo.MsgStatusEnum;
import openbang.account.service.vo.UserLoginInfo;
import openbang.account.web.portal.widget.DateGwtHelper;
import openbang.account.web.portal.widget.LazyLoading;
import openbang.account.web.portal.widget.MsgListWidget;
import openbang.account.web.portal.widget.SendMsgWidget;
import openbang.account.web.portal.widget.UserBaseInfoMaintainWidget;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedStackPanel;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * 帐户中心的控制
 * 要求：必须先登录
 * 
 * 1、如果没有登录，提示必须先登录，跳到登录界面
 * 2、登录后，跳到该页面
 * 3、查看和修改 个人基本信息
 * 4、查看、回复和删除短信
 * 5、发短信
 * 6、我的项目（来源于：项目管理系统的一些查询提示）
 * 7、我的任务（来源于：项目管理系统的一些查询提示）
 * 8、我的BUG（来源于：BUG管理系统的一些查询提示）
 * 9、
 * 
 * 界面布局
 * 
 * 昵称(email)
 * 登录日期  退出登录
 * 
 * 菜单项（上面）                         
 * 
 * 
 * @author aibozeng
 *
 */

public class Portal implements EntryPoint {
	
	/**
	 * 左边菜单区域
	 */
	VerticalPanel vPanelMenu = new VerticalPanel();
	
	/**
	 * 右边工作区
	 */
	DecoratedTabPanel tabPanel = new DecoratedTabPanel();
	
	private UserLoginInfo userLoginInfo = null;

	//private static SendMsgWidget sendMsgWidget = null;  延迟加载 javascript
	//private static MsgListWidget msgReceListWidget = null;
	//private static MsgListWidget msgArchListWidget = null;
	//private MsgHandler msgHandler = new PortalMsgHandler();
	
	@Override
	public void onModuleLoad() {
		RootPanel.get("portal_menu").add(vPanelMenu);
		RootPanel.get("portal_content").add(tabPanel);
        
		AccountService.Util.getInstance().getLoginUserInfo(null,new AsyncCallback<UserLoginInfo>(){
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}
			public void onSuccess(UserLoginInfo result) {
				userLoginInfo = result;
				processLoginUser();
				RootPanel.get("loading").setVisible(false);
			}
		});
	}
	
	public void processLoginUser(){
		if(!userLoginInfo.isLogined()){
			Window.alert("您还没有登录,请先登录.");
			toSwitchLoginUrl();
		}
		else{
			
			//处理界面组件
			vPanelMenu.setStyleName("titleTxt");
			vPanelMenu.add(new HTML("welcome "+userLoginInfo.getNiceName()));
			vPanelMenu.add(new HTML("["+userLoginInfo.getEmail()+"]"));
			vPanelMenu.add(new HTML("登录日期:"+ DateGwtHelper.format(userLoginInfo.getLoginDate())));
			//vPanelMenu.add(new HTML("退出:"));
			DecoratedStackPanel stackPanel = new DecoratedStackPanel();
		    stackPanel.setWidth("170px");
		    //几个抽屉
		    stackPanel.add(createPersonBox(),getHeaderString("个人信息","/images/person.jpg"),true);			
		    stackPanel.add(createMsgBox(),getHeaderString("我的消息","/images/mail.jpg"),true);	
		    stackPanel.add(createProjectBox(),getHeaderString("我的项目","/images/proj.jpg"),true);	
		    stackPanel.add(createTaskBox(),getHeaderString("我的任务","/images/task.jpg"),true);	
		    
		    vPanelMenu.add(new HTML("<hr>"));
		    vPanelMenu.add(stackPanel);
		    
		    //工作区的 Tab
		    tabPanel.setStyleName("titleTxt");
		    tabPanel.setWidth("100%");
		    tabPanel.setAnimationEnabled(true);
		    //tabPanel.setHeight("400px");
		    
		}
	}
	
	//抽屉  个人信息
	public Widget createPersonBox(){
		VerticalPanel vPanelPerson = new VerticalPanel();
		vPanelPerson.setStyleName("contentTxt");
		HTML baseInfo = new HTML("<a href=\"#\">基本信息</a>");
		HTML contractInfo = new HTML("联系信息");
		HTML addrInfo = new HTML("地址信息");
		vPanelPerson.add(baseInfo);
		vPanelPerson.add(contractInfo);
		vPanelPerson.add(addrInfo);
		baseInfo.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				addTabWidget("基本信息");
			}
		});
		return vPanelPerson;
	}
	
	//抽屉  我的消息
	public Widget createMsgBox(){
		VerticalPanel vPanelPerson = new VerticalPanel();
		vPanelPerson.setStyleName("contentTxt");
		HTML receiveBox = new HTML("<a href=\"#\">收件箱</a>");
		HTML archiveBox = new HTML("<a href=\"#\">存档箱</a>");
		HTML iSend = new HTML("<a href=\"#\">发消息</a>");
		vPanelPerson.add(receiveBox);
		vPanelPerson.add(archiveBox);
		vPanelPerson.add(iSend);
		iSend.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {	
				addTabWidget("发消息");
			}
		});
		receiveBox.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {	
				addTabWidget("收件箱");
			}
		});
		archiveBox.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {	
				addTabWidget("存档箱");
			}
		});
		return vPanelPerson;
	}
	
	//抽屉  我的项目
	public Widget createProjectBox(){
		VerticalPanel vPanelPerson = new VerticalPanel();
		vPanelPerson.setStyleName("contentTxt");
		HTML myProjBox = new HTML("我参与的项目");
		HTML interestProjBox = new HTML("我关注的项目");
		vPanelPerson.add(myProjBox);
		vPanelPerson.add(interestProjBox);
		return vPanelPerson;
	}

	//抽屉   我的任务
	public Widget createTaskBox(){
		VerticalPanel vPanelPerson = new VerticalPanel();
		vPanelPerson.setStyleName("contentTxt");
		HTML pendingAssiBox = new HTML("待分配的任务");
		HTML pendingProcBox = new HTML("待处理的任务");
		HTML finishBox = new HTML("已完成的任务");
		vPanelPerson.add(pendingAssiBox);
		vPanelPerson.add(pendingProcBox);
		vPanelPerson.add(finishBox);
		return vPanelPerson;
	}
	
	//工作区页面的记录  记录已经放到 Tab 的标题内容
	ArrayList<String> tabStr = new ArrayList<String>();
	
	//工作区的页面的增加
	public void addTabWidget(String tabTitle){
		//查询是否已经存在
		int index=0;
		for(String str : tabStr){
			if(str.equals(tabTitle)){
				//已经存在
				tabPanel.selectTab(index);
				return;
			}
			index++;
		}
		//不存在
		if("基本信息".equals(tabTitle)){
			GWT.runAsync(new LazyLoading(tabTitle){
			    public void onSuccess() {
					tabPanel.add(new UserBaseInfoMaintainWidget(),something);
					tabStr.add(something);
					tabPanel.selectTab(tabStr.size()-1);	
			    }
			});
			return;//异步的
		}
		else if("发消息".equals(tabTitle)){
			GWT.runAsync(new LazyLoading(tabTitle){
			    public void onSuccess() {
					tabPanel.add(new SendMsgWidget(),something);
					tabStr.add(something);
					tabPanel.selectTab(tabStr.size()-1);	
			    }
			});	
			//异步的
		}
		else if("收件箱".equals(tabTitle)){
			GWT.runAsync(new LazyLoading(tabTitle){
			    public void onSuccess() {
					tabPanel.add(new MsgListWidget(),something);
					tabStr.add(something);
					tabPanel.selectTab(tabStr.size()-1);	
			    }
			});
			//异步的
		}
		else if("存档箱".equals(tabTitle)){
			GWT.runAsync(new LazyLoading(tabTitle){
			    public void onSuccess() {
					tabPanel.add(new MsgListWidget(MsgStatusEnum.HAS_ARCHIVE.getCode()),something);
					tabStr.add(something);
					tabPanel.selectTab(tabStr.size()-1);	
			    }
			});		
			//异步的
		}
		else{
		}
	}
	
	public static native void toSwitchLoginUrl() /*-{  
         parent.toLoginUrl();
    }-*/;

	
	/**
	   * Get a string representation of the header that includes an image and some
	   * text.
	   * 
	   * @param text the header text
	   * @param image the {@link ImageResource} to add next to the header
	   * @return the header as a string
	   */
	  private String getHeaderString(String text, String imageUrl) {
	    // Add the image and text to a horizontal panel
	    HorizontalPanel hPanel = new HorizontalPanel();
	    hPanel.setSpacing(0);
	    hPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
	    if(imageUrl!=null){
	       hPanel.add(new Image(imageUrl));
	    }
	    HTML headerText = new HTML(text);
	    headerText.setStyleName("openbang-StackPanelHeader");
	    hPanel.add(headerText);
	    // Return the HTML string for the panel
	    return hPanel.getElement().getString();
	  }
	
 /*	  
	  private class PortalMsgHandler implements MsgHandler{

		@Override
		public void listMsg(int msgStatus) {
		}

		@Override
		public void listMsg(List<MsgReceive> msgs) {
		}

		@Override
		public void replyMsg(MsgAsyn msg) {
			initSendMsgWidget();
			sendMsgWidget.replyMsg(msg);
			addTabWidget("发消息");
		}

		@Override
		public void sendNewMsg() {
			initSendMsgWidget();
			sendMsgWidget.sendNewMsg();
			addTabWidget("发消息");
		}
	  }
	  public void initSendMsgWidget(){
			if(sendMsgWidget==null){
				sendMsgWidget = new SendMsgWidget();
			}
		  
	  }
*/
}
